package com.yc.jpyy.penaltydoc.service.impl;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.penaltydoc.dao.PenaltyDocDao;
import com.yc.jpyy.penaltydoc.service.PenaltyDocService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class PenaltyDocServiceImpl extends BaseService implements PenaltyDocService {

    private static Object sync = new Object();

    @Autowired
    private PenaltyDocDao penaltyDocDao;
    @Override
    public List<Map<String, Object>> getDriverCarRelationByVehicleNo(String vehicleNo) {
        return this.penaltyDocDao.getDriverCarRelationByVehicleNo(vehicleNo);
    }

    @Override
    public String getMaxNumByType(Map<String, Object> paramsMap) {
        //普货道路运输证号：130181000001至130181599999
        //客车道路运输证号：130181600001至130181699999
        //出租道路运输证号：130181700001至130181799999
        //危货道路运输证号：130181800001至130181899999

        //*1:登记编号；2:受理编号；3.许可编号；4.个人经营许可证号；
        //* 5.普货道路运输证号；6：服务证号；7：卷宗号(货运)，
        //* 8 客车道路运输证号；9 出租道路运输证号；
        //* 10 危货道路运输证号
        //* 11 网约车档案袋编号；12 巡游车档案袋编号； 13 固定资产编号；
        //* 14 日常用品编号； 15 行政处罚档案； 16 默认编号
    	//* 17 服务监督卡号； 18 巡游车经营许可证； 18网约车经营许可证
    	//* 20 巡游车道路运输证； 21 网约车道路运输证； 
    	String serialNumberType = (String) paramsMap.get("serialnumbertype");
    	String regionNo = "";
        String cityName = "";
        String maxNum = "";
        int num = this.getMaxNum(paramsMap);
        Date date = new Date();
        DecimalFormat decimalFormat = new DecimalFormat("000000");
        DecimalFormat decimalFormat1 = new DecimalFormat("0000");
        LocalDate now = LocalDate.now();
        String year = String.valueOf(now.getYear()).substring(2);
        switch (serialNumberType){
            case "DJBH":
                maxNum = cityName + "(登记)字【" + date.getTime()+ "】" + decimalFormat.format(num) + "号";
                break;
            case "SLBH":
                maxNum = cityName + "(受理)字【" + date.getTime() + "】" + decimalFormat.format(num) + "号";
                break;
            case "XKBH":
                maxNum = cityName + "(许可)字【" + date.getTime()  + "】" + decimalFormat.format(num) + "号";
                break;
            case "XKZH":   //个人经营许可证号
            	regionNo = penaltyDocDao.getRegionNo(paramsMap);
                maxNum = regionNo + year + decimalFormat1.format(num);
                break;
            case "PHDLYSZH":
                if (num >= 599999)
                {
                    maxNum = "";
                }else{
                    maxNum = "130181" + decimalFormat.format(num);
                }
                break;
            case "FWZH":
                maxNum = "XJC" + decimalFormat.format(num);
                break;
            case "KCDLYSZH":
                if (num >= 99999)
                {
                    maxNum = "";
                }else{
                    maxNum = "1301816" + decimalFormat.format(num);
                }
                break;
            case "CZDLYSZH":
                if (num >= 99999)
                {
                    maxNum = "";
                }else{
                    maxNum = "1301817" + decimalFormat.format(num);
                }
                break;
            case "WHDLYSZH":
                if (num >= 99999)
                {
                    maxNum = "";
                }else{
                    maxNum = "1301818" + decimalFormat.format(num);
                }
                break;

            case "WYCDADBH"://网约车档案袋编号
                maxNum = "WWC" + decimalFormat.format(num);
                break;
            case "XYCDADBH"://巡游车档案袋编号
                maxNum = "XYC" + decimalFormat.format(num);
                break;
            case "GDZCBH"://固定资产编号
                maxNum = "ZCGD" + decimalFormat.format(num);
                break;
            case "RCYPBH"://日常用品编号
                maxNum = "ZCRC" + decimalFormat.format(num);
                break;
            case "XZCFDA"://行政处罚档案
                maxNum = "XZCF" + decimalFormat.format(num);
                break;
            case "FWJDKH"://服务监督卡号
                maxNum = "T" + decimalFormat.format(num);
                break;
            case "XYCJYXKZ"://巡游车经营许可证
            	regionNo = penaltyDocDao.getRegionNo(paramsMap);
                maxNum = regionNo + year + decimalFormat1.format(num);
                break;
            case "WYCJYXKZ"://网约车经营许可证
            	regionNo = penaltyDocDao.getRegionNo(paramsMap);
                maxNum = regionNo + year + decimalFormat1.format(num);
                break;
            case "XYCDLYSZ"://巡游车道路运输证
            	//去掉前面的130
            	regionNo = penaltyDocDao.getRegionNo(paramsMap).substring(3);;
                maxNum = regionNo + year + decimalFormat1.format(num);
                //查询该号是否已被使用（递归）
                num = isUseNo(num,maxNum,serialNumberType,regionNo,year,decimalFormat1);
                penaltyDocDao.updateNum(serialNumberType, num);
                maxNum = regionNo + year + decimalFormat1.format(num);
                break;
            case "WYCDLYSZ"://网约车道路运输证	
            	//去掉前面的130
            	regionNo = penaltyDocDao.getRegionNo(paramsMap).substring(3);
                maxNum = regionNo + year + decimalFormat1.format(num);
                //查询该号是否已被使用（递归）
                num = isUseNo(num,maxNum,serialNumberType,regionNo,year,decimalFormat1);
                penaltyDocDao.updateNum(serialNumberType, num);
                maxNum = regionNo + year + decimalFormat1.format(num);
                break;
            default:
                maxNum = decimalFormat.format(num);
                break;
        }
        return maxNum;
    }

    /**
     * 获取最大号
     * @param serialNumberType
     * @return
     */
    private  int getMaxNum(Map<String, Object> paramsMap){
    	String serialNumberType = (String) paramsMap.get("serialnumbertype");
    	String dbcode = (String) paramsMap.get("dbcode");
        int i = 0;
        synchronized (sync) {
        	//查询呢改类型是否存在
        	int isHave =  penaltyDocDao.checkIsHave(serialNumberType);
        	if(isHave > 0){
        		i = penaltyDocDao.getMaxNum(serialNumberType) + 1;
        		penaltyDocDao.updateNum(serialNumberType, i);
        	}else{
        		i = 0;
        		penaltyDocDao.addNum(serialNumberType, i,dbcode);
        	}
        }
        return i;
    }
    
    
    /**
     * 查询道路运输证号是否已经使用
     */
    private  int isUseNo(int num,String maxNum,String serialNumberType,
    		             String regionNo,String year,DecimalFormat decimalFormat1){
        int i = 0;
        synchronized (sync) {
        	 i = penaltyDocDao.isUseByVehicle(maxNum);
             if(i > 0){
            	maxNum = regionNo + year + decimalFormat1.format(num + 1);
             	return isUseNo(num + 1,maxNum,serialNumberType,regionNo,year,decimalFormat1);
             }else{
            	return num; 
             }
        } 
    }
}
